package com.gc.baselib.img;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.TransitionOptions;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.gc.baselib.img.progress.OnProgressListener;


import java.io.File;
import java.util.concurrent.ExecutionException;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * Created by zk on 2019/6/20.
 */

public final class ImageLoader {
    private IImageLoader defaultImageLoader;
    private ImageLoader(){
        defaultImageLoader=new GildeImageLoaderImpl();
    }
    /**静态内部类单例*/
    public static ImageLoader getmInstance(){
        return ImageLoaderInner.mInstance;
    }

    public  static void init(Context context){
        ImageLoader.getmInstance().defaultImageLoader.init(context);
    }
    public static void trimMemory(int level) {
        ImageLoader.getmInstance().defaultImageLoader.trimMemory(level);
    }
    public static void clearMemory() {
        ImageLoader.getmInstance().defaultImageLoader.clearMemory();
    }
    public static File getCacheDir(){
       return ImageLoader.getmInstance().defaultImageLoader.getCacheDir();
    }

    /**
     * 同步获取bitmap从缓存中获取
     * @param url
     * @return
     */
    public Bitmap getBitmapFromCache(String url) throws ExecutionException, InterruptedException {
        return  ImageLoader.getmInstance().defaultImageLoader.getBitmapFromCache(url);
    }

    public void getBitmapFromCache(Context context, String url, IGetBitmapListener listener){
        ImageLoader.getmInstance().defaultImageLoader.getBitmapFromCache(context,url,listener);
    }
    /**
     * 清除磁盘缓存
     */
    public static void clearDiskCache(){
        ImageLoader.getmInstance().defaultImageLoader.clearDiskCache();
    }

    public static void load(Activity activity,String url,ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,url);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(Fragment fragment,String url,ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(View view, String url, ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,url);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(Activity activity, String url, ImageView.ScaleType scaleType, ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,url).scaleType(scaleType);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(Activity activity,String url,ImageView imageView,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,url).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(Fragment fragment,String url,ImageView imageView,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(View view,String url,ImageView imageView,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,url).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }

    public static void load(Fragment fragment,String url, ImageView.ScaleType scaleType,ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).scaleType(scaleType);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(View view, String url,  ImageView.ScaleType scaleType,ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,url).scaleType(scaleType);
        ImageLoader.getmInstance().displayImage(config);
    }

    public static void load(Activity activity, String url, ImageView imageView,int radius ,int width, int height,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,url)
                                .radius(radius).size(width,height).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(Fragment fragment, String url, ImageView imageView,int radius, int width, int height,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url)
                .radius(radius).size(width,height).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(View view, String url, ImageView imageView, int width,int radius, int height,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,url)
                .radius(radius).size(width,height).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void load(View view, int resId, ImageView imageView, int width,int radius, int height,@DrawableRes int placeholder,@DrawableRes int error){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,resId)
                .radius(radius).size(width,height).placehoder(placeholder).errorRes(error);
        ImageLoader.getmInstance().displayImage(config);
    }
    /**
     * 圆形图片的裁剪
     * @param activity
     * @param url
     * @param imageView
     */
    public static void loadRound(Activity activity, String url, ImageView imageView,int radius){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,url).radius(radius);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadRound(Fragment fragment, String url, ImageView imageView, int radius){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).radius(radius);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadRound(View view, String url, ImageView imageView, int radius){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,url).radius(radius);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadRound(Activity activity, int  resId, ImageView imageView,int radius){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,resId).radius(radius);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadRound(Fragment fragment, int resId, ImageView imageView, int radius){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,resId).radius(radius);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadRound(View view, int resId, ImageView imageView, int radius){
        ImageLoadConfig config=new ImageLoadConfig(view,imageView,resId).radius(radius);
        ImageLoader.getmInstance().displayImage(config);
    }
    /**
     * 圆形图片的裁剪
     * @param context
     * @param url
     * @param imageView
     * @param defRes
     */
    public static void loadCircle(Context context, String url, ImageView imageView, int defRes) {
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,url).placehoder(defRes).tranformation(new CircleCrop());
        ImageLoader.getmInstance().displayImage(config);
    }

    public static void loadCircle(Fragment fragment, String url, ImageView imageView, int defRes) {
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).tranformation(new CircleCrop());
        ImageLoader.getmInstance().displayImage(config);
    }


    public static void loadCircle(Activity fragment, String url, ImageView imageView, int defRes) {
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).tranformation(new CircleCrop());
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadCircle(Context context, int resId, ImageView imageView, int defRes) {
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,resId).placehoder(defRes).tranformation(new CircleCrop());
        ImageLoader.getmInstance().displayImage(config);
    }

    public static void loadCircle(Fragment fragment, int resId, ImageView imageView, int defRes) {
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,resId).placehoder(defRes).tranformation(new CircleCrop());
        ImageLoader.getmInstance().displayImage(config);
    }


    public static void loadCircle(Activity fragment, int resId, ImageView imageView, int defRes) {
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,resId).placehoder(defRes).tranformation(new CircleCrop());
        ImageLoader.getmInstance().displayImage(config);
    }


    /***
     * 模糊加载
     * @param context
     * @param resId
     * @param blurRadius
     * @param defRes
     */
    public static void loadBlur(Context context, int resId, ImageView imageView, int defRes, int blurRadius){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,resId).placehoder(defRes).tranformation(new BlurTransformation(blurRadius));
        ImageLoader.getmInstance().displayImage(config);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadBlur(Fragment context, int resId, ImageView imageView, int defRes, int blurRadius){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,resId).placehoder(defRes).tranformation(new BlurTransformation(blurRadius));
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadBlur(Activity context, int resId, ImageView imageView, int defRes, int blurRadius){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,resId).placehoder(defRes).tranformation(new BlurTransformation(blurRadius));
        ImageLoader.getmInstance().displayImage(config);
        ImageLoader.getmInstance().displayImage(config);
    }

    public static void loadBlur(Context context, String url, ImageView imageView, int defRes, int blurRadius){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,url).placehoder(defRes).tranformation( new BlurTransformation(blurRadius));
        ImageLoader.getmInstance().displayImage(config);
    }

    public static void loadBlur(Fragment fragment, String url, ImageView imageView, int defRes, int blurRadius){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).tranformation(new BlurTransformation(blurRadius));
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadBlur(Activity fragment, String url, ImageView imageView, int defRes, int blurRadius){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).tranformation(new BlurTransformation(blurRadius));
        ImageLoader.getmInstance().displayImage(config);
    }

    /**
     * 加载失败加载另外一张图片
     * @param fragment
     * @param url
     * @param fallbackUrl
     * @param imageView
     */
    public static void loadErrorReload(Fragment fragment, String url, String fallbackUrl, ImageView imageView, int defRes){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).errorUrl(fallbackUrl);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadErrorReload(Activity activity, String url, String fallbackUrl, ImageView imageView, int defRes){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,url).placehoder(defRes).errorUrl(fallbackUrl);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static  void loadErrorReload(Context context, String url, String fallbackUrl, ImageView imageView, int defRes){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,url).placehoder(defRes).errorUrl(fallbackUrl);
        ImageLoader.getmInstance().displayImage(config);
    }


    //    TransitionOptions 用于给一个特定的请求指定过渡。
//    每个请求可以使用 RequestBuilder 中的 transition()
//    方法来设定 TransitionOptions 。还可以通过使用
//    BitmapTransitionOptions 或 DrawableTransitionOptions
//    来指定类型特定的过渡动画。对于 Bitmap 和 Drawable
//    之外的资源类型，可以使用 GenericTransitionOptions。 Glide v4 将不会默认应用交叉淡入或任何其他的过渡效果。每个请求必须手动应用过渡。
    public void loadTransition(Context context, String url, TransitionOptions transitionOptions, ImageView imageView){

    }
    public void loadTransition(Activity activity, String url, TransitionOptions transitionOptions, ImageView imageView){

    }
    public void loadTransition(Fragment fragment, String url, TransitionOptions transitionOptions, ImageView imageView){

    }


    //如果没有两个url的话，也想，记载一个缩略图
    public void loadThumbnail(Fragment fragment, String url, float thumbnailSize, ImageView imageView){}
    public void loadThumbnail(Activity activity, String url, float thumbnailSize, ImageView imageView){}
    public void loadThumbnail(Context context, String url, float thumbnailSize, ImageView imageView){}

    public void loadProgressListener(Context context, String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener){}
    public void loadProgressListener(Activity activity, String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener){}
    public void loadProgressListener(Fragment fragment, String url, ImageView imageView, int placeholderResId, int errorResId, OnProgressListener onProgressListener){}


    //add shiming   2018.4.20 transformation 需要装换的那种图像的风格，错误图片，或者是，正在加载中的错误图
    public void loadResTransform(Activity activity, int resId, ImageView imageView, Transformation transformation, int errorResId){}
    public void loadResTransform(Context context, int resId, ImageView imageView, Transformation transformation, int errorResId){}
    public void loadResTransform(Fragment fragment, int resId, ImageView imageView, Transformation transformation, int errorResId){}

    //这是对网络图片，进行的图片操作，使用的glide中的方法
    public void loadTransform(Context context, String url, ImageView imageView, int defRes, Transformation transformation){}
    public void loadTransform(Fragment fragment, String url, ImageView imageView, int defRes, Transformation transformation){}
    public void loadTransform(Activity activity, String url, ImageView imageView, int defRes, Transformation transformation){}

    public static void  loadLoaderListener(Context context, String url, ImageView imageView, int defRes, IImageLoaderListener listener){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,url).placehoder(defRes).listener(listener);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadLoaderListener(Fragment fragment, String url, ImageView imageView, int defRes, IImageLoaderListener listener){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).listener(listener);
        ImageLoader.getmInstance().displayImage(config);
    }
    public static void loadLoaderListener(Activity fragment, String url, ImageView imageView, int defRes, IImageLoaderListener listener){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,url).placehoder(defRes).listener(listener);
        ImageLoader.getmInstance().displayImage(config);
    }

    public void displayImageInResource(Context context, int resId, ImageView imageView, Transformation transformations){};




    /**
     *  停止图片的加载，对某一个的Activity
     * @hide
     */
    public static void stopLoad(Activity activity, ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(activity,imageView,null);
        ImageLoader.getmInstance().defaultImageLoader.stopLoad(config);
    }
    /**
     * 停止图片的加载，context
     * {@hide}
     */
    public void stopLoad(Context context, ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(context,imageView,null);
        ImageLoader.getmInstance().defaultImageLoader.stopLoad(config);
    }
    /**
     * 停止图片的加载，fragment
     * {@hide}
     */
    public void stopLoad(Fragment fragment, ImageView imageView){
        ImageLoadConfig config=new ImageLoadConfig(fragment,imageView,null);
        ImageLoader.getmInstance().defaultImageLoader.stopLoad(config);
    }

    //失去焦点，建议实际的项目中少用，取消求情
    public void pauseRequests(Context context){
        ImageLoadConfig config=new ImageLoadConfig().context(context);
        ImageLoader.getmInstance().defaultImageLoader.pauseRequests(config);
    }
    public void pauseRequests(Activity activity){
        ImageLoadConfig config=new ImageLoadConfig().context(activity);
        ImageLoader.getmInstance().defaultImageLoader.pauseRequests(config);
    }
    public void pauseauseRequests(Fragment fragment){
        ImageLoadConfig config=new ImageLoadConfig().context(fragment);
        ImageLoader.getmInstance().defaultImageLoader.pauseRequests(config);
    }

    //获取焦点，建议实际的项目中少用
    public void resumeRequests(Context context){
        ImageLoadConfig config=new ImageLoadConfig().context(context);
        ImageLoader.getmInstance().defaultImageLoader.resumeRequests(config);
    }
    public void resumeRequests(Activity activity){
        ImageLoadConfig config=new ImageLoadConfig().context(activity);
        ImageLoader.getmInstance().defaultImageLoader.resumeRequests(config);
    }
    public void resumeRequests(Fragment fragment){
        ImageLoadConfig config=new ImageLoadConfig().context(fragment);
        ImageLoader.getmInstance().defaultImageLoader.resumeRequests(config);
    }

    public void displayImage(ImageLoadConfig builder){
        defaultImageLoader.displayImage(builder);
    }

    private static class ImageLoaderInner{
        private static ImageLoader mInstance=new ImageLoader();
    }

}
